//
//  XzxyybVC+UI.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import Foundation

extension XzxyybVC {
    
    func initUI() {
        
        let top_view = UIImageView.init(image: #imageLiteral(resourceName: "beijing2@1.5x"))
        top_view.isUserInteractionEnabled = true
        view.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(UIDevice.lgf_NavBarH())
        }
        self.navTitle = "新增样本"
        self.lgf_NavigationBar.style.lgf_TitleColor = UIColor.white
        self.lgf_NavigationBar.lgf_Reload()
        
        scrollView = UIScrollView.lgf_Table("FFFFFF")
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.automaticallyAdjustsScrollViewInsets = false
        }
        view.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(top_view.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        ypwj = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        ypwj.lgf_ViewName = "新增样本"
        ypwj.delegate = self
        scrollView.addSubview(ypwj)
        ypwj.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let ypwj_l = UILabel.init()
        ypwj_l.text = "新增样本"
        ypwj_l.textColor = UIColor.init(lgf_HexString: "222222")
        ypwj_l.font = UIFont.boldSystemFont(ofSize: 16)
        ypwj.addSubview(ypwj_l)
        ypwj_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let ypwj_x = UILabel.init()
        ypwj_x.text = "*"
        ypwj_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        ypwj_x.font = UIFont.boldSystemFont(ofSize: 16)
        ypwj.addSubview(ypwj_x)
        ypwj_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        xcbg = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        xcbg.lgf_ViewName = "心超报告"
        xcbg.delegate = self
        scrollView.addSubview(xcbg)
        xcbg.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(ypwj.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let xcbg_l = UILabel.init()
        xcbg_l.text = "心超报告"
        xcbg_l.textColor = UIColor.init(lgf_HexString: "222222")
        xcbg_l.font = UIFont.boldSystemFont(ofSize: 16)
        xcbg.addSubview(xcbg_l)
        xcbg_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        bh = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "自动生成", text: "", false)
        bh.lgf_ViewName = "编号"
        bh.isUserInteractionEnabled = false
        scrollView.addSubview(bh)
        bh.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(xcbg.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let bh_l = UILabel.init()
        bh_l.text = "编号"
        bh_l.textColor = UIColor.init(lgf_HexString: "222222")
        bh_l.font = UIFont.boldSystemFont(ofSize: 16)
        bh.addSubview(bh_l)
        bh_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        jcsj = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        jcsj.lgf_ViewName = "检查时间"
        jcsj.delegate = self
        scrollView.addSubview(jcsj)
        jcsj.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(bh.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let jcsj_l = UILabel.init()
        jcsj_l.text = "检查时间"
        jcsj_l.textColor = UIColor.init(lgf_HexString: "222222")
        jcsj_l.font = UIFont.boldSystemFont(ofSize: 16)
        jcsj.addSubview(jcsj_l)
        jcsj_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        mzh = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "请输入", text: "", false)
        mzh.lgf_ViewName = "身高"
        mzh.textField.keyboardType = .numberPad
        scrollView.addSubview(mzh)
        mzh.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(jcsj.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let mzh_l = UILabel.init()
        mzh_l.text = "身高"
        mzh_l.textColor = UIColor.init(lgf_HexString: "222222")
        mzh_l.font = UIFont.boldSystemFont(ofSize: 16)
        mzh.addSubview(mzh_l)
        mzh_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        zyh = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "请输入", text: "", false)
        zyh.lgf_ViewName = "体重"
        zyh.textField.keyboardType = .numberPad
        scrollView.addSubview(zyh)
        zyh.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(mzh.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let zyh_l = UILabel.init()
        zyh_l.text = "体重"
        zyh_l.textColor = UIColor.init(lgf_HexString: "222222")
        zyh_l.font = UIFont.boldSystemFont(ofSize: 16)
        zyh.addSubview(zyh_l)
        zyh_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        tw = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        tw.lgf_ViewName = "体位"
        tw.delegate = self
        scrollView.addSubview(tw)
        tw.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(zyh.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let tw_l = UILabel.init()
        tw_l.text = "体位"
        tw_l.textColor = UIColor.init(lgf_HexString: "222222")
        tw_l.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_l)
        tw_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let tw_x = UILabel.init()
        tw_x.text = "*"
        tw_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        tw_x.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_x)
        tw_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        phcd = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        phcd.lgf_ViewName = "配合程度"
        phcd.delegate = self
        scrollView.addSubview(phcd)
        phcd.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(tw.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let phcd_l = UILabel.init()
        phcd_l.text = "配合程度"
        phcd_l.textColor = UIColor.init(lgf_HexString: "222222")
        phcd_l.font = UIFont.boldSystemFont(ofSize: 16)
        phcd.addSubview(phcd_l)
        phcd_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let phcd_x = UILabel.init()
        phcd_x.text = "*"
        phcd_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        phcd_x.font = UIFont.boldSystemFont(ofSize: 16)
        phcd.addSubview(phcd_x)
        phcd_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        sfzj = segmentView(phcd, "是否镇静", ["否", "是"])
        
        cjcs = LGFLoginImageTitleView.init(nil, #imageLiteral(resourceName: "icon_arrow"), left: 80, placeholder: "请选择", text: "", true)
        cjcs.lgf_ViewName = "采集场所"
        cjcs.delegate = self
        scrollView.addSubview(cjcs)
        cjcs.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(sfzj.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let cjcs_l = UILabel.init()
        cjcs_l.text = "采集场所"
        cjcs_l.textColor = UIColor.init(lgf_HexString: "222222")
        cjcs_l.font = UIFont.boldSystemFont(ofSize: 16)
        cjcs.addSubview(cjcs_l)
        cjcs_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let cjcs_x = UILabel.init()
        cjcs_x.text = "*"
        cjcs_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        cjcs_x.font = UIFont.boldSystemFont(ofSize: 16)
        cjcs.addSubview(cjcs_x)
        cjcs_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        xczd = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "请输入", text: "", false)
        scrollView.addSubview(xczd)
        xczd.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(cjcs.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let xczd_l = UILabel.init()
        xczd_l.text = "心超诊断"
        xczd_l.textColor = UIColor.init(lgf_HexString: "222222")
        xczd_l.font = UIFont.boldSystemFont(ofSize: 16)
        xczd.addSubview(xczd_l)
        xczd_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        lczd = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "请输入", text: "", false)
        scrollView.addSubview(lczd)
        lczd.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(xczd.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let lczd_l = UILabel.init()
        lczd_l.text = "临床诊断"
        lczd_l.textColor = UIColor.init(lgf_HexString: "222222")
        lczd_l.font = UIFont.boldSystemFont(ofSize: 16)
        lczd.addSubview(lczd_l)
        lczd_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let label3 = UILabel.init()
        label3.text = "心音听诊描述及诊断"
        label3.textColor = UIColor.init(lgf_HexString: "222222")
        label3.font = UIFont.boldSystemFont(ofSize: 16)
        scrollView.addSubview(label3)
        label3.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(lczd.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        xytzms = UITextView.init()
        xytzms.delegate = self
        xytzms.font = UIFont.systemFont(ofSize: 16)
        xytzms.backgroundColor = UIColor.init(lgf_HexString: "F6F9FB")
        scrollView.addSubview(xytzms)
        xytzms.snp.makeConstraints { (make) in
            make.top.equalTo(label3.snp.bottom)
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.height.equalTo(200)
        }
        xytzms_p = UILabel.init()
        xytzms_p.text = "请填写心音听诊描述及诊断"
        xytzms_p.textColor = UIColor.init(lgf_HexString: "CFD4D9")
        scrollView.addSubview(xytzms_p)
        xytzms_p.snp.makeConstraints { (make) in
            make.top.equalTo(label3.snp.bottom).offset(7)
            make.left.equalToSuperview().offset(22)
        }
        
        let bc = UIButton.init(type: .custom)
        bc.backgroundColor = UIColor.init(lgf_HexString: EWEDarkMode.c_0F88F2)!
        bc.setTitle("保存", for: .normal)
        bc.setTitleColor(EWEDarkMode.white(), for: .normal)
        bc.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        bc.layer.cornerRadius = 4
        bc.layer.masksToBounds = true
        bc.addTarget(self, action: #selector(ZzscjlVC.s_bc), for: .touchUpInside)
        scrollView.addSubview(bc)
        bc.snp.makeConstraints { (make) in
            make.top.equalTo(xytzms.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
            make.width.equalToSuperview().offset(-75)
            make.height.equalTo(50)
        }
        
        scrollView.lgf_AutoContentSize(30)
    }
    
    func segmentView(_ view: UIView, _ title: String, _ data: [String]) -> LGFSegmentView {
        let tw = LGFLoginImageTitleView.init(nil, nil, left: 80, placeholder: "", text: "", true)
        scrollView.addSubview(tw)
        tw.snp.makeConstraints { (make) in
            make.width.equalToSuperview().offset(-30)
            make.centerX.equalToSuperview()
            make.top.equalTo(view.snp.bottom).offset(10)
            make.height.equalTo(44.lgf_H())
        }
        let tw_l = UILabel.init()
        tw_l.text = title
        tw_l.textColor = UIColor.init(lgf_HexString: "222222")
        tw_l.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_l)
        tw_l.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
        
        let tw_x = UILabel.init()
        tw_x.text = "*"
        tw_x.textColor = UIColor.init(lgf_HexString: "CE4C4C")
        tw_x.font = UIFont.boldSystemFont(ofSize: 16)
        tw.addSubview(tw_x)
        tw_x.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(-10)
            make.centerY.equalToSuperview().offset(2)
        }
        
        let segment = LGFSegmentView.init()
        segment.layer.cornerRadius = 15
        tw.addSubview(segment)
        segment.snp.makeConstraints { (make) in
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.equalTo(30)
            make.width.equalTo(120)
        }
        
        DispatchQueue.main.async {
            segment.setData(data: data, font: UIFont.systemFont(ofSize: 14), selectColor: UIColor.init(lgf_HexString: "0F88F2"), unSelectColor: UIColor.init(lgf_HexString: "E0E6EB"), sTextColor: UIColor.white, unsTextColor: UIColor.white, dx: false)
        }
        
        return segment
    }
    
}

extension XzxyybVC: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        if textView == xytzms {
            xytzms_p.isHidden = textView.text.count > 0
        }
    }
}

extension XzxyybVC: LGFLoginImageTitleViewDelegate {
    func selectLGFLoginImageTitleView(_ view: LGFLoginImageTitleView) {
        self.view.endEditing(true)
        if view.lgf_ViewName == "新增样本" {
            DispatchQueue.main.async {
                self.lgf_ShowFile("public.audio")
                LGFFileUpload.lgf_GetFileData = { (file) in
                    let fileName = file.components(separatedBy: ".").last
                    let fileFullName = file.components(separatedBy: "/").last
                    if fileName == "wav" {
                        EWERequest.uploadFile([file], "30", "wav") { [weak self] (path) in
                            self?.ypwj.lgf_Name = path
                            self?.ypwj.text = fileFullName
                            let pathFullName = fileFullName!.components(separatedBy: ".").first
                            self?.bh.text = (EWEUser.lgf.abbreviation ?? "") + "-" + (pathFullName ?? "")
                        } _: { (error) in
                            SVProgressHUD.showError(error)
                        }
                    } else {
                        SVProgressHUD.showError("新增样本文件仅支持 wav")
                    }
                }
            }
        } else if view.lgf_ViewName == "心超报告" {
            DispatchQueue.main.async {
                self.lgf_ShowFile("com.adobe.pdf")
                LGFFileUpload.lgf_GetFileData = { (file) in
                    let fileName = file.components(separatedBy: ".").last
                    let fileFullName = file.components(separatedBy: "/").last
                    if fileName == "pdf" {
                        EWERequest.uploadFile([file], "30", "pdf") { [weak self] (path) in
                            self?.xcbg.lgf_Name = path
                            self?.xcbg.text = fileFullName
                        } _: { (error) in
                            SVProgressHUD.showError(error)
                        }
                    } else {
                        SVProgressHUD.showError("心超报告文件仅支持 pdf")
                    }
                }
            }
        } else if view.lgf_ViewName == "检查时间" {
            let picker = LGFBottomDatePickerView()
            picker.showIn(self.view, .date, "yyyy-MM-dd") { (view, date, timestamp) in
                self.jcsj.text = timestamp.description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd")
                self.jcsj.lgf_Name = timestamp.description
            }
        } else if view.lgf_ViewName == "体位" {
            let picker = LGFBottomPickerView()
            let sindex = self.twArr.lgf_IndexOfObject(self.tw.textField.text ?? "")
            picker.showIn(self.view, self.twArr, sindex) { [weak self] (view, name, index) in
                self?.tw.text = self?.twArr[index]
                self?.tw.lgf_Name = self?.twIdArr[index]
            }
        } else if view.lgf_ViewName == "配合程度" {
            let picker = LGFBottomPickerView()
            let sindex = self.phcdArr.lgf_IndexOfObject(self.phcd.textField.text ?? "")
            picker.showIn(self.view, self.phcdArr, sindex) { [weak self] (view, name, index) in
                self?.phcd.text = self?.phcdArr[index]
                self?.phcd.lgf_Name = self?.phcdIdArr[index]
            }
        } else if view.lgf_ViewName == "采集场所" {
            let picker = LGFBottomPickerView()
            let sindex = self.cjcsArr.lgf_IndexOfObject(self.cjcs.textField.text ?? "")
            picker.showIn(self.view, self.cjcsArr, sindex) { [weak self] (view, name, index) in
                self?.cjcs.text = self?.cjcsArr[index]
                self?.cjcs.lgf_Name = self?.cjcsIdArr[index]
            }
        }
    }
}
